package transervfx.Dato.DAO;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import transervfx.Dato.DAO.Dao;
import transervfx.Dato.DAO.PersistenceManager;
import transervfx.Dato.Entidad.Encomienda;
import transervfx.Dato.Entidad.Servicio;
import transervfx.Dato.Entidad.Tramite;

public class ServicioDAO implements Dao<Servicio, Long> {

    private static ServicioDAO INSTANCE = null;

    
    protected ServicioDAO() {
    }

    /**
     * creador sincronizado para protegerse de posibles problemas multi-hilo
     * otra prueba para evitar instanciación múltiple
     */
    private synchronized static void createInstance() {
        if (INSTANCE == null) {
            INSTANCE = new ServicioDAO();
        }
    }

    /**
     * Obtiene la instancia.
     *
     * @return devuelve la instancia del "GestorAuxiliar".
     */
    public static ServicioDAO getInstance() {
        if (INSTANCE == null) {
            createInstance();
        }
        return INSTANCE;
    }

    @Override
    public void persist(Servicio entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {
            EntityTransaction t = em.getTransaction();
            try {
                t.begin();
                em.persist(entity);
                t.commit();
            } finally {
                if (t.isActive()) {
                    t.rollback();
                }
            }
        } finally {
            em.close();
        }
    }

    
    @Override
    public void remove(Servicio entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            EntityTransaction t = em.getTransaction();
            try {
                t.begin();
                System.out.println("ESTAS POR ELIMIAR");
                entity = em.getReference(Servicio.class, entity.getId());
                em.remove(entity);
                System.out.println("LO ELIMINA");
                t.commit();
            } finally {
                if (t.isActive()) {
                    t.rollback();
                }
            }
        } finally {
            em.close();
        }
    }

    @Override
    public void update(Servicio entity) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {


            em.getTransaction().begin();
            System.out.println("Entra a modificar el servicio");
            em.merge(entity);

            em.getTransaction().commit();
            System.out.println("Lo modifica al Servicio");

        } finally {
            em.close();
        }
    }
    
    @Override
    public Servicio findById(Long id) {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.getReference(Servicio.class, id);

        } finally {
            em.close();
        }
    }

    @Override
    public List<Servicio> findAll() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("Servicio.findAll").getResultList();

        } finally {
            em.close();
        }
    }
    
    public List<Encomienda> findAllEncomienda() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("Encomienda.findAll").getResultList();

        } finally {
            em.close();
        }
    }

    public List<Tramite> findAllTramite() {
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();
        try {

            return em.createNamedQuery("Tramite.findAll").getResultList();

        } finally {
            em.close();
        }
    }

}

